/**
 * 用户权限管理 所需要的state。
 */

import { stringify } from 'querystring';
import { history } from 'umi';
import { message } from 'antd';
import {
  fetchUserData,
  createUser,
  updateUser,
  deleteUser,
  fetchDepartsData,
  createDepart,
  updateDepart,
  deleteDepart,
  fetchAccessData,
  fetchRolesData,
  createRole,
  updateRole,
  deleteRole,
  createCompany,
} from '../services/rbacService';
import { encrypt, fetchTree } from '../utils/utils';
import { cloneDeep, isEmpty, forEach } from 'lodash';

const Model = {
  namespace: 'enoadRBAC', // 命名空间
  state: {
    dataSource: [],
    departSource: [],
    departData: [],
    accessData: [], //所有权限数据。
  }, // store 数据
  effects: {
    //获取用户表数据信息。
    *fetchUserData({ payload }, { call, put }) {
      const response = yield call(fetchUserData, { ...payload });
      if (response.code === 200 && response.data) {
        let dataSource = response.data;
        forEach(dataSource, (item) => {
          item.key = item._id;
        });
        yield put({
          type: 'changeRBACStatus',
          payload: { dataSource },
        });
      }
    },
    //创建新用户
    *createUser({ payload }, { call }) {
      //获取当前登录用户
      let storage = window.sessionStorage;
      let userDate = storage.getItem('user');
      if (userDate) {
        userDate = JSON.parse(userDate);
      }
      payload['companyId'] = userDate?.data?.company?._id;

      payload.password = encrypt(payload.password);
      const response = yield call(createUser, { ...payload });
      if (response.code === 200 && response.data) {
        return response;
      } else {
        message.error(response.msg);
        return null;
      }
    },
    //更新用户
    *updateUser({ payload }, { call }) {
      payload.password = encrypt(payload.password);
      const response = yield call(updateUser, { ...payload });
      if (response.code === 200 && response.data) {
        return response;
      } else {
        message.error(response.msg);
        return null;
      }
    },

    //删除用户（可以删除一个，也可以删除一组。[id,id]）
    *deleteUser({ payload }, { call }) {
      const response = yield call(deleteUser, { ...payload });
      if (response.code === 200 && response.data) {
        return response;
      } else {
        message.error(response.msg);
        return null;
      }
    },

    //--------------部门-----------------
    *fetchDepartsData({ payload }, { call, put }) {
      const response = yield call(fetchDepartsData, { ...payload });
      if (response.code === 200 && response.data) {
        let departSource = fetchTree(response.data);
        let departData = response.data;

        yield put({
          type: 'changeRBACStatus',
          payload: { departSource, departData },
        });
      } else {
        message.error(response.msg);
      }
    },
    *createDepart({ payload }, { call }) {
      const response = yield call(createDepart, { ...payload });
      if (response.code === 200 && response.data) {
        return response;
      } else {
        message.error(response.msg);
        return null;
      }
    },
    *updateDepart({ payload }, { call }) {
      let storage = window.sessionStorage;
      let userDate = storage.getItem('user');
      if (userDate) {
        userDate = JSON.parse(userDate);
      }
      payload['user'] = JSON.stringify(userDate && userDate.data) || '';

      payload['departUpdateTime'] = new Date().getTime();
      payload['departUpdateBy'] = JSON.stringify({
        id: userDate && userDate.data._id,
        name: userDate && userDate.data.name,
      });

      const response = yield call(updateDepart, { ...payload });
      if (response.code === 200 && response.data) {
        return response;
      } else {
        message.error(response.msg);
        return null;
      }
    },

    *deleteDepart({ payload }, { call }) {
      const response = yield call(deleteDepart, { ...payload });
      if (response.code === 200 && response.data) {
        return response;
      } else {
        message.error(response.msg);
        return null;
      }
    },

    //--------------角色-----------------

    *fetchAccessData({ payload }, { call, put }) {
      const response = yield call(fetchAccessData, { ...payload });
      if (response.code === 200 && response.data) {
        let accessData = response.data;

        yield put({
          type: 'changeRBACStatus',
          payload: { accessData },
        });
        return accessData;
      } else {
        message.error(response.msg);
      }
    },

    *fetchRolesData({ payload }, { call, put }) {
      const response = yield call(fetchRolesData, { ...payload });
      if (response.code === 200 && response.data) {
        return response.data;
      } else {
        message.error(response.msg);
        return null;
      }
    },
    *createRole({ payload }, { call }) {
      const response = yield call(createRole, { ...payload });
      if (response.code === 200 && response.data) {
        return response;
      } else {
        message.error(response.msg);
        return null;
      }
    },
    *updateRole({ payload }, { call }) {
      const response = yield call(updateRole, { ...payload });
      if (response.code === 200 && response.data) {
        return response;
      } else {
        message.error(response.msg);
        return null;
      }
    },

    *deleteRole({ payload }, { call }) {
      const response = yield call(deleteRole, { ...payload });
      if (response.code === 200 && response.data) {
        return response;
      } else {
        message.error(response.msg);
        return null;
      }
    },

    *createCompany({ payload }, { call }) {
      const response = yield call(createCompany, { ...payload });
      if (response.code === 200 && response.data) {
        return response;
      } else {
        message.error(response.msg);
        return null;
      }
    },
  },

  reducers: {
    // effect获取数据处理方法
    changeRBACStatus(state, { payload }) {
      return { ...state, ...payload };
    },
  },
};
export default Model;
